package com.cskaoyan.service.user;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.user.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: zhijie
 * @create: 2021-11-29 22:05
 **/
@Service
public class UserManageImpl implements UserManageService {

    @Autowired
    MallUserMapper mallUserMapper;
    @Autowired
    MallAddressMapper mallAddressMapper;
    @Autowired
    MallCollectMapper mallCollectMapper;
    @Autowired
    MallFootprintMapper mallFootprintMapper;
    @Autowired
    MallSearchHistoryMapper mallSearchHistoryMapper;
    @Autowired
    MallFeedbackMapper mallFeedbackMapper;

    /**
     * 根据username,mobile模糊查询
     *
     * @param username
     * @param mobile
     * @param userId
     * @param pageBo
     * @return
     */
    @Override
    public UserListData userList(String username, String mobile, String userId, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallUserExample example = new MallUserExample();
        MallUserExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        if (mobile != null && !StringUtils.isEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        if (username != null && !StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<MallUser> mallUsers = mallUserMapper.selectByExample(example);
        //得到查询的数量
        PageInfo<MallUser> info = new PageInfo<>(mallUsers);
        long total = info.getTotal();
        int pages = info.getPages();
        UserListData data = new UserListData();
        data.setLimit(pageBo.getLimit());
        data.setPage(pageBo.getPage());
        data.setPages(pages);
        data.setTotal(total);
        data.setList(mallUsers);
        return data;
    }

    /**
     * 根据id精准查询用户
     *
     * @param id
     * @return
     */
    @Override
    public MallUser userDetail(String id) {
        MallUser mallUser = mallUserMapper.selectByPrimaryKey(Integer.parseInt(id));
        return mallUser;
    }

    /**
     * 根据id,name模糊查询地址信息
     *
     * @param userId
     * @param name
     * @param pageBo
     * @return
     */
    @Override
    public AddressListVo addressList(String userId, String name, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallAddressExample example = new MallAddressExample();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        MallAddressExample.Criteria criteria = example.createCriteria();
        if (userId != null && !StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (name != null && !StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MallAddress> addressList = mallAddressMapper.selectByExample(example);

        //得到查询的数量
        PageInfo<MallAddress> info = new PageInfo<>(addressList);
        long total = info.getTotal();
        int pages = info.getPages();
        AddressListVo addressListVo = new AddressListVo();
        addressListVo.setLimit(pageBo.getLimit());
        addressListVo.setPage(pageBo.getPage());
        addressListVo.setPages(pages);
        addressListVo.setTotal(total);
        addressListVo.setList(addressList);
        return addressListVo;
    }

    /**
     * 根据userId 和valueId查询用户收藏
     *
     * @param userId
     * @param valueId
     * @param pageBo
     * @return
     */
    @Override
    public CollectListVO collectList(String userId, String valueId, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallCollectExample example = new MallCollectExample();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        MallCollectExample.Criteria criteria = example.createCriteria();
        if (userId != null && !StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (valueId != null && !StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        List<MallCollect> mallCollects = mallCollectMapper.selectByExample(example);
        //得到查询的数量
        PageInfo<MallCollect> info = new PageInfo<>(mallCollects);
        long total = info.getTotal();
        int pages = info.getPages();
        CollectListVO vo = new CollectListVO();
        vo.setLimit(pageBo.getLimit());
        vo.setPage(pageBo.getPage());
        vo.setPages(pages);
        vo.setTotal(total);
        vo.setList(mallCollects);
        return vo;
    }

    /**
     * 根据userId 和goodsId查询用户足迹
     *
     * @param userId
     * @param goodsId
     * @param pageBo
     * @return
     */
    @Override
    public FootPrintVo footprintList(String userId, String goodsId, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallFootprintExample example = new MallFootprintExample();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        MallFootprintExample.Criteria criteria = example.createCriteria();
        if (userId != null && !StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (goodsId != null && !StringUtils.isEmpty(goodsId)) {
            criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
        }
        criteria.andDeletedEqualTo(false);
        List<MallFootprint> mallFootprints = mallFootprintMapper.selectByExample(example);
        //得到查询的数量
        PageInfo<MallFootprint> info = new PageInfo<>(mallFootprints);
        long total = info.getTotal();
        int pages = info.getPages();
        FootPrintVo vo = new FootPrintVo();
        vo.setLimit(pageBo.getLimit());
        vo.setPage(pageBo.getPage());
        vo.setPages(pages);
        vo.setTotal(total);
        vo.setList(mallFootprints);
        return vo;
    }

    /**
     * 根据userId 和keyword查询用户搜索历史
     *
     * @param userId
     * @param keyword
     * @param pageBo
     * @return
     */
    @Override
    public HistoryListVo historyList(String userId, String keyword, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallSearchHistoryExample example = new MallSearchHistoryExample();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        MallSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (userId != null && !StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (keyword != null && !StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MallSearchHistory> mallSearchHistories = mallSearchHistoryMapper.selectByExample(example);
        //得到查询的数量
        PageInfo<MallSearchHistory> info = new PageInfo<>(mallSearchHistories);
        long total = info.getTotal();
        int pages = info.getPages();
        HistoryListVo vo = new HistoryListVo();
        vo.setLimit(pageBo.getLimit());
        vo.setPage(pageBo.getPage());
        vo.setPages(pages);
        vo.setTotal(total);
        vo.setList(mallSearchHistories);
        return vo;
    }

    @Override
    public FeedbackListVo feedbackList(String id, String username, PageBo pageBo) {
        //在查询之前使用分页
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallFeedbackExample example = new MallFeedbackExample();
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        MallFeedbackExample.Criteria criteria = example.createCriteria();
        if (id != null && !StringUtils.isEmpty(id)) {
            criteria.andUserIdEqualTo(Integer.parseInt(id));
        }
        if (username != null && !StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<FeedbackData> mallFeedbacks = mallFeedbackMapper.selectByExampleArray(example);
        //得到查询的数量
        PageInfo<FeedbackData> info = new PageInfo<>(mallFeedbacks);
        long total = info.getTotal();
        int pages = info.getPages();
        FeedbackListVo vo = new FeedbackListVo();
        vo.setLimit(pageBo.getLimit());
        vo.setPage(pageBo.getPage());
        vo.setPages(pages);
        vo.setTotal(total);
        vo.setList(mallFeedbacks);
        return vo;
    }

    @Override
    public int userUpdate(Byte status, Byte userLevel, Integer id) {
        MallUser mallUser = new MallUser();
        mallUser.setId(id);
        mallUser.setStatus(status);
        mallUser.setUserLevel(userLevel);
        mallUser.setUpdateTime(new Date());
        int i = mallUserMapper.updateByPrimaryKeySelective(mallUser);
        return i;
    }
}
